package net.jueb.sniff4j.impl.http.base;

import net.jueb.sniff4j.extr.base.AbstractChannelCodecInit;
import net.jueb.sniff4j.extr.util.HttpObjectAggregator;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * 代理过程中的2个通道初始化类
 * @author Administrator
 *
 */
public class HttpChannelCodecInit extends AbstractChannelCodecInit{
	
	@Override
	public void codecInit(Channel proxyClientChannel, Channel proxyTargeChannel) {
		//####################代理服务器于客户端请求的目标主机之间的配置########################
		proxyTargeChannel.pipeline().addLast(HttpResponseDecoder.class.getName(),new HttpResponseDecoder());
		//添加编码器，为proxyClientChannel向targer发送数据时编码
		proxyTargeChannel.pipeline().addLast(HttpRequestEncoder.class.getName(),new HttpRequestEncoder());
		//TODO //HttpObjectAggregator负责将1个HTTP请求消息的多个部分合并成一条完整的HTTP消息,参数为ContentLength最大允许长度
		proxyTargeChannel.pipeline().addLast(HttpObjectAggregator.class.getName(),new HttpObjectAggregator(1048576));
		//将目标发来的数据送入客户端管道,放入之前返回信息被重构
		
		//####################代理服务器与客户端之间的配置########################
		//添加解码器，为客户端向代理服务发送数据时解码
		proxyClientChannel.pipeline().addLast(HttpRequestDecoder.class.getName(),new HttpRequestDecoder());
		//TODO 当有数据发往客户端时，编码为HttpResponse
		proxyClientChannel.pipeline().addLast(HttpResponseEncoder.class.getName(),new HttpResponseEncoder());
		 //HttpObjectAggregator负责将1个HTTP请求消息的多个部分合并成一条完整的HTTP消息,参数为ContentLength最大允许长度
		proxyClientChannel.pipeline().addLast(HttpObjectAggregator.class.getName(),new HttpObjectAggregator(1048576));
	}
}
